﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;
using BPM.Base.Tools;

namespace BPM.Foundation.PWService
{
    [System.ServiceModel.ServiceBehavior(InstanceContextMode = System.ServiceModel.InstanceContextMode.Single)]
    public partial  class PowerService : BPM.Foundation.PWService.IPowerService
    {
        public bool TestConnect()
        {
            return true;
        }

        //[构造函数]
        public PowerService(PWDBDataContext PWDBDataContext)
        {
            if (PWDBDataContext == null)
            {
                throw new MyException("PWDBDataContext为空");
            }
            else
            {
                PWDBDataContext.DeferredLoadingEnabled = true;
                this.PWDB = PWDBDataContext;
            }
        }

        //[10.1	Linq对象]
        private PWDBDataContext PWDB;

        //[10.2	保存数据到数据库]
        private int? submit()
        {
            //[1]
            try
            {
                PWDB.SubmitChanges();
                return null;
            }
            catch (System.Exception ex)
            {
                MyLog.writeError(ex.Message);
                return -2000;
            }
        }

        //[10.3	口令加密]
        private string passwordMD5(string password)
        {
            return password;// +"MD5";
        }

        //[10.4	业务数据类型验证]
        private bool typeValidate(string value, string type)
        {
            if (PWDB.SYS_RegularExpression.Count(p => p.typeNo == type) == 1)
            {
                var v = PWDB.SYS_RegularExpression.Single(p => p.typeNo == type);

                return MyValidate.validate(value, v.expressionNet);
            }

            else
            {

                return true;
            }
        }

        //[10.5	系统内置数据保护开关]
        private bool checkKey(string key)
        {
            List<string> ks = new List<string> { "system", "all", "none", "director", "structure", "subagent", "officeuser" };
            foreach (string s in ks)
            {

                int i = String.Compare(s, key, true);
                if (i == 0)
                {
                    return true;
                }
            }

            return false;
        }

        //[10.6  创建扩展信息]
        private int? createExpansionTable(string businessKey, string expansionTableName)
        {
            try
            {
                PWDB.createExpansionTable(businessKey, expansionTableName);
                return null;
            }
            catch (System.Exception ex)
            {
                MyLog.writeError(ex.Message);
                return -3000;
            }
        }

        //[10.8 为  [5.16得到下级所有部门列表]  提供的递归算法]
        private void addChildDepartment(PW_Department parent, List<PW_Department> listResult, string pid)
        {
            List<PW_Department> list = getChildDepartment(pid, parent.departmentNo);

            if (list == null)
            {
                return;
            }
            listResult.AddRange(list);
            foreach (var obj in list)
            {
                addChildDepartment(obj, listResult, pid);
            }
        }

        //[10.7 为  [5.13得到部门树]  提供的递归算法]
        private void addDepartmentTree(DepartmentTree parent, string pid)
        {

            List<PW_Department> list = getChildDepartment(pid, parent.current.departmentNo);

            if (list == null)
            {
                return;
            }

            foreach (var obj in list)
            {
                DepartmentTree d = new DepartmentTree();
                d.current = obj;
                d.parent = getParentDepartment(pid, obj.departmentNo);
                d.childList = new List<DepartmentTree>();
                if (d.current.parentDepartment != d.current.departmentNo)
                {
                    parent.childList.Add(d);
                    addDepartmentTree(d, pid);
                }
            }




        }
    }
}
